#!/bin/bash
## kola:
##   # Increase timeout since this test has a lot of I/O and involves rebasing
##   timeoutMin: 20
##   # We've seen some OOM when 1024M is used:
##   # https://github.com/coreos/fedora-coreos-tracker/issues/1506
##   minMemory: 2048
##   # This test only runs on FCOS due to a problem with skopeo copy on
##   # RHCOS. See: https://github.com/containers/skopeo/issues/1846
##   distros: fcos
##   # Needs internet access as we fetch files from koji
##   tags: "needs-internet platform-independent"
##   description: Verify that build of a container image with a new kernel
##     and reboot into it succeeds.

#
# Copyright (C) 2023 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

set -euxo pipefail

# shellcheck disable=SC1091
. "$KOLA_EXT_DATA/commonlib.sh"

cd $(mktemp -d)

# TODO: It'd be much better to test this via a registry
image_dir=/var/tmp/fcos
image=oci:$image_dir
image_pull=ostree-unverified-image:$image
tmp_imagedir=/var/tmp/fcos-tmp
arch=$(arch)
kver="6.2.9-300.fc38.${arch}"

case "${AUTOPKGTEST_REBOOT_MARK:-}" in
  "")
    # Take the existing ostree commit, and export it to a container image, then rebase to it.
    rpm-ostree status --json > status.json
    checksum=$(jq -r '.deployments[0].checksum' < status.json)
    v0=$(jq -r '.deployments[0].version' < status.json)
    imgref=$(jq -r '.deployments[0]["container-image-reference"]' < status.json)
    rm ${image_dir} -rf
    encapsulate_args=()
    # A hack...if we're booted into a container, then we need to fake things out
    # for the merge commit to turn it back into an image.  What we *really* want
    # here obviously is seamless support for re-serializing a container from
    # the ostree storage, but right now we're not doing the tar-split stuff.
    if [[ "$imgref" != null ]]; then
      encapsulate_args+=("--label" "ostree.bootable=true")
    fi
    # Since we're switching OS update stream, turn off zincati
    systemctl mask --now zincati
    ostree container encapsulate "${encapsulate_args[@]}" --repo=/ostree/repo ${checksum} "${image}"
    # This one keeps --experimental, but we also test without it below
    rpm-ostree rebase --experimental "$image_pull"
    ostree container image list --repo=/ostree/repo | tee imglist.txt
    # Test rebasing back to ostree https://github.com/coreos/rpm-ostree/issues/3677
    rpm-ostree rebase "$checksum"
    rpm-ostree rebase "$image_pull"
    /tmp/autopkgtest-reboot 1
    ;;
  1)
    # Setup
    # copy the OCI dir to containers-storage for a local build
    skopeo copy $image containers-storage:localhost/fcos
    rm "${image_dir}" -rf
    td=$(mktemp -d)
    cd ${td}
    version=$(rpm-ostree --version | grep Version)
cat > Dockerfile << EOF
FROM localhost/fcos
RUN rpm-ostree cliwrap install-to-root /
RUN rpm-ostree override replace \
    https://koji.fedoraproject.org/koji/buildinfo?buildID=2178613 && \
    ostree container commit
EOF
    # Older podman found in RHEL8 blows up without /etc/resolv.conf
    # which happens in our qemu path.
    touched_resolv_conf=0
    if test '!' -f /etc/resolv.conf; then
      podmanv=$(podman --version)
      case "${podmanv#podman version }" in
        3.*) touched_resolv_conf=1; touch /etc/resolv.conf;;
      esac
    fi
    podman build --net=host -t localhost/fcos-derived --squash .
    if test "${touched_resolv_conf}" -eq 1; then
      rm -vf /etc/resolv.conf
    fi
    derived=oci:$image_dir:derived
    skopeo copy containers-storage:localhost/fcos-derived $derived
    rpm-ostree --version
    rpm-ostree rebase ostree-unverified-image:$derived
    ostree container image list --repo=/ostree/repo
    rm $image_dir -rf
    /tmp/autopkgtest-reboot 2
    ;;
  2)
    un=$(uname -r)
    if test "$un" != "$kver"; then
      echo "Expected kernel $kver but found $un"
      exit 1
    fi
    test -f /usr/lib/modules/$kver/initramfs.img
    test -f /usr/lib/modules/$kver/vmlinuz
esac
